/**
 * 
 */
package edu.sjtu.seda.stage;

import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import edu.sjtu.seda.stage.api.StageConfig;
import edu.sjtu.seda.stage.api.StageConfiguration;

/**
 * @author john.zhu
 *
 */
public class StageConfigLoader{
	
	public static StageConfig getStageConfig(String stage){
		StageConfiguration mainConfig = getStageConfiguration();
		List<StageConfig> configs = mainConfig.getStageconfig();
		if(configs!=null&&!configs.isEmpty()){
			for(StageConfig config :configs){
				if(config.getStage().equals(stage)){
					return config;
				}
			}
		}
		return null;
	}
	
	public static List<StageConfig> getStageConfigList(){
		return getStageConfigList("StageConfiguration.xml");
	}
	
	public static List<StageConfig> getStageConfigList(String file){
		return getStageConfiguration(file).getStageconfig();
	}
	
	private static StageConfiguration getStageConfiguration(String file){
		JAXBContext _context = null;
		try {
			_context = JAXBContext.newInstance(StageConfiguration.class);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		
		Unmarshaller unMarshaller;
		try {
			unMarshaller = _context.createUnmarshaller();

			return (StageConfiguration) unMarshaller.unmarshal(ClassLoader.getSystemResourceAsStream(file));
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static StageConfiguration getStageConfiguration(){
		return getStageConfiguration("StageConfiguration.xml");
	}
	
}
